const app = angular.module('app', []);
app.filter('imageSize', function () {
    return input => {
        if (input < 1000) {
            return input + "B"
        } else if (input >= 1000 && input < 1000000) {
            return (input / 1000).toFixed(2) + "KB"
        } else if (input >= 1000000 && input < 1000000000) {
            return (input / 1000000).toFixed(2) + "MB"
        } else if (input >= 1000000000) {
            return (input / 1000000000).toFixed(2) + "GB"
        } else {
            return input;
        }
    }
})
app.filter('imageId', function () {
    return input => {
        if (input.startsWith("sha256:")) {
            return input.split("sha256:")[1].substring(0, 12)
        } else {
            return input
        }
    }
})
app.controller('ctrl', ['$scope', '$http', ($scope, $http) => {
    $scope.images = []
    $scope.init = () => {
        $http.get(`/image/list`).then(res => {
            $scope.images = res.data
        })
    }
    $scope.deleteImage = id => {
        $http.delete(`/image/` + id).then(() => {
            alert('删除成功')
            $scope.init()
        }, err => {
            alert(err.data.message)
        })
    }
    $scope.details = {}
    $scope.detailsVisible = false
    $scope.imageDetails = data => {
        $scope.details = data
        $("#imageDetailsModal").modal('show')
        $scope.detailsVisible = true
    }
    $scope.imagePrune = () => {
        $http.get(`/image/prune`).then(() => {
            alert('清理成功')
            $scope.init()
        })
    }
    $scope.imageName = ''
    $scope.imagePull = () => {
        $http.get(`/image/pull?name=` + $scope.imageName).then(() => {
            alert('正在拉取，请稍后')
            $scope.init()
        })
    }
    $scope.init()
}])